﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Xunit;
namespace LeetCode.CodeDay
{
    public partial class Code
    {
        /// <summary>
        /// 日历
        /// </summary>
        /// <param name="args"></param>
        public void DateTable()
        {
            Action<string> Print = (str) => Console.WriteLine(str);
            string str = "2023-9";
            while (str != "exit")
            {
                if (string.IsNullOrWhiteSpace(str) == false)
                {
                    Print($@"输入了：{str}");

                    var date = str.Split("-").Where(_ => string.IsNullOrWhiteSpace(_) == false).ToList();
                    if (date.Count != 2 && date.Count != 3)
                        Print($@"输入格式错误");
                    var yearStr = date.First();
                    var monthStr = date[1];
                    string dayStr = null;
                    if (date.Count == 3)
                        dayStr = date[2];
                    if (int.TryParse(yearStr, out int year) && int.TryParse(monthStr, out int month))
                    {
                        bool hasDay = int.TryParse(dayStr, out int thisDay);
                        if (hasDay == false)
                        {
                            Print($@"输入格式错误,正确格式【2021-03-26】");
                        }

                        if (year < 1970 || month < 1 || month > 12 || year > 5000)
                            Print($@"输入格式错误,正确格式【2021-03】,最大5000年");
                        else
                        {
                            Print($@"输入数据，年份：【{year}】，月份：【{month}】");

                            Func<int, bool> isRunNian = (a) => a % 400 == 0 || a % 4 == 0 && a % 100 != 0;

                            bool thisIsRunNian = isRunNian(year);

                            int startYear = 1970;
                            int days = 0;
                            var currentMonthDays = 30;
                            while (startYear < year)
                            {
                                days += (isRunNian(startYear) ? 366 : 365);
                                startYear++;
                            }
                            var bigMonth = new HashSet<int> { 1, 3, 5, 7, 8, 10, 12 };
                            var allMonth = Enumerable.Range(1, 12);


                            foreach (var curMonth in allMonth)
                            {

                                var curMonthDays = 30;
                                if (bigMonth.Contains(curMonth))
                                {
                                    curMonthDays = 31;
                                }
                                else
                                {
                                    if (curMonth == 2)
                                        curMonthDays = (thisIsRunNian ? 29 : 28);
                                }
                                if (curMonth == month)
                                {
                                    currentMonthDays = curMonthDays;
                                }
                                if (curMonth > month - 1)
                                    break;
                                days += curMonthDays;
                            }
                            if (hasDay && thisDay > currentMonthDays)
                            {
                                Print($@"输入格式错误,{month}月没有{thisDay}天");
                                str = Console.ReadLine();
                                continue;
                            }
                            SortedDictionary<int, string> weekDict = new SortedDictionary<int, string>
                            {
                                { 1, "星期一" },
                                { 2, "星期二" },
                                { 3, "星期三" },
                                { 4, "星期四" },
                                { 5, "星期五" },
                                { 6, "星期六" },
                                { 7, "星期日" }
                            };
                            int week = (days + 4) % 7;
                            Console.WriteLine($@"{weekDict[week == 0 ? 7 : week]}");
                            foreach (var weekStr in weekDict)
                            {
                                Console.Write($"{weekStr.Value}");
                                Console.Write($"    ");
                            }
                            Console.WriteLine();
                            Func<string, int, string> getStr = (str, i1) =>
                            {
                                var sl = str.Length;
                                if (sl < i1)
                                {
                                    var bChar = i1 - sl;
                                    for (int i = 0; i < bChar; i++)
                                    {
                                        str += " ";
                                    }
                                }
                                return str;
                            };
                            for (int i = 1; i < currentMonthDays + week; i++)
                            {
                                if (i < week)
                                {
                                    Console.Write(getStr("", 10));
                                }
                                else
                                {
                                    var printDay = i - week + 1;
                                    if (printDay == thisDay)
                                        Console.Write(getStr($"[{printDay}]", 10));
                                    else
                                        Console.Write(getStr($"{printDay}", 10));
                                }
                                if (i % 7 == 0)
                                    Console.WriteLine();
                            }
                            Console.WriteLine();
                        }

                    }
                    else
                    {
                        Print($@"输入格式错误,正确格式【2021-03】");
                    }

                }
                str = Console.ReadLine();
            }
            Print($@"正在退出...");
            Thread.Sleep(3000);
        }


    }
}